package com.axjy.util;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PagerInterceptor implements HandlerInterceptor{

    /**
     * 相当于自定义Controller的前置增强
     * @param req
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String _offset=req.getParameter("offset");
        String _limit=req.getParameter("limit");
        Integer offset=0;
        Integer pageSize=5;
        if(_offset!=null){
            offset=Integer.valueOf(_offset);
        }
        if(_limit!=null){
            pageSize=Integer.valueOf(_limit);
        }
        PagerContext.setOffsetTl(offset);
        PagerContext.setPageSizeTl(pageSize);
        return true;
    }


    /**
     * 相遇于后置增强
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        PagerContext.destroy();
    }


    /**
     * 页面渲染完成
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
